'''
 Long numbers can be made to look nicer, so let’s write some code to do just that.

You should write a function for converting a number to string using several rules. First of all, you will need to cut the number with a given base (base argument; default 1000). The value is a float number with decimal after the point (decimals argument; default 0). For the value, use the rounding towards zero rule (5.6⇒5, -5.6⇒-5) if the decimal = 0, otherwise use the standard rounding procedure. If the number of decimals is greater than the current number of digits after dot, trail value with zeroes. The number should be a value with letters designating the power. You will be given a list of power designations (powers argument; default ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']). If you are given suffix (suffix argument; default ‘’) , then you must append it. If you don’t have enough powers - stay at the maximum. And zero is always zero without powers, but with suffix.

Let's look at examples. It will be simpler.

    n=102
    result: "102", the base is default 1000 and 102 is lower this base.
    n=10240
    result: "10k", the base is default 1000 and rounding down.
    n=12341234, decimals=1
    result: "12.3M", one digit after the dot.
    n=12000000, decimals=3
    result: "12.000M", trailing zeros.
    n=12461, decimals=1
    result: "12.5k", standard rounding.
    n=1024000000, base=1024, suffix='iB'
    result: '976MiB', the different base and the suffix.
    n=-150, base=100, powers=['', 'd', 'D']
    result: '-1d', the negative number and rounding towards zero.
    n=-155, base=100, decimals=1, powers=['', 'd', 'D']
    result: '-1.6d', the negative number and standard rounding.
    n=255000000000, powers=['', 'k', 'M']
    result: '255000M', there is not enough powers.
'''

def friendly_number(number, base=1000, decimals=0, suffix='',
                    powers=['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']):
    """
    Format a number as friendly text, using common suffixes.
    """
    # 检索powers列表，取出长度，即进位极限len(powers)
    # 比较number和base，确定应该进几次位carry，即number/base几次后将小于base
    carry = 0
    for i in range(0, len(powers)):
        if number / (base ** i) < base:
            carry = i
            break
        carry = i
    # 被除数的base变成base**(carry)
    base = base ** carry
    # 使用number除以base，保留decimals位
    # round(number,decimals)
    # 但是在decimals=0的情况下直接使用int或者//
    if decimals == 0:
        number = number // base
    else:
        number = round(number/base, decimals)
    # number和powers(carry+1)和suffix连起来，就是结果
    print(''.join([str(number), powers[carry], suffix]))
    return ''.join([str(number), powers[carry], suffix])

#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
    assert friendly_number(102) == '102', '102'
    assert friendly_number(10240) == '10k', '10k'
    assert friendly_number(12341234, decimals=1) == '12.3M', '12.3M'
    assert friendly_number(12461, decimals=1) == '12.5k', '12.5k'
    assert friendly_number(1024000000, base=1024, suffix='iB') == '976MiB', '976MiB'
    friendly_number(12000000, decimals=3)  #float('%.03f'%a)

